<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
label.error{
margin-left: 10px;
color: red;
}
</style>

<script type="text/javascript">

	function initDialog(){
		//$('a.ui-dialog-titlebar-close').hide();
		$('#modal-simple').dialog({
		    autoOpen: false,
		    width: 400,
		    open: function(event, ui) {
		        //隱藏「x」關閉按鈕
		        $(this).parent().children().children('.ui-dialog-titlebar-close').hide();
		    },
		    buttons: {
		        "OK": function () {
		            $(this).dialog("close");
		            location.href="<%=g_basePath%>/storemgr/storeList.htm";
		        },
		        "Cancel": function () {
		            $(this).dialog("close");
		            location.href="<%=g_basePath%>/storemgr/storeDetail.htm";
		        },
		        
		        
		    },
		    closeText: "Cancel" 
		});
	}
	
	//初始化数据，为要修改信息的店铺初始化数据，
	function initData(){

		var storeId="${store.id}";
		if(typeof(storeId)!="undefined" && storeId!=""){
			$("#id").val("${store.id}");
			$("#code").val("${store.code}");
			$("#name").val("${store.name}");
			$("#addr").val("${store.addr}");
			$("#tel").val("${store.tel}");
			//alert("${store.osw}");
			//alert("${store.osw}");
			//alert($("#osw").find("option:selected").text());
			//$("#osw option[val='store']").attr("selected", true); 
		}
	}
	$(document).ready(function() {
		initDialog();
		initData();
		$("#myForm").validate({
			rules: {
				code:{
					required: true,
					maxlength:10
				},
				name:{
					required: true,
					maxlength:50
				},
				addr:{
					maxlength:50
				},
				tel:{
					maxlength:50
				}
			},
			highlight: function(element) {
				
			},
			success: function(element) {
				
			},
			submitHandler:function() { 
				//alert("Submitted!"); 
				var options = { 
						success: function(data) { 
							//alert(data);
							if(data=="true"){
								$('#modal-simple').dialog('open');
								//alert('<spring:message code="store.add.success"></spring:message>'); 
								
								
							}else{
								alert("other msg");
							}
							
						} 
				}; 
				$('#myForm').ajaxSubmit(options);
			}
	    });
		
		
		var msgCode='<c:out value="${msg}"></c:out>';
		if(msgCode!="" && msgCode=="fail"){
			
			var msg='<spring:message code="store.add.fail"></spring:message>';
			alert(msg);
			location.href="<%=g_basePath%>/storemgr/storeDetail.htm";
		}
	});


</script>


</head>
<body>
	
	<form class="form-horizontal" role="form" method="post" id="myForm" name="myForm" action="<%=g_basePath%>/storemgr/saveStore.htm">
	<div class="col-md-11" role="main">
		<input type="text" id="id" name="id" style="display: none;">
	  <div class="form-group">
	    <label class="col-sm-2 control-label"><spring:message code="store.code"></spring:message></label>
	    <div class="col-sm-3">
	      <input type="text" class="form-control required" id="code" name="code" placeholder="<spring:message code="store.code"></spring:message>">
	    </div>
	  </div>
	  <div class="form-group">
	    <label class="col-sm-2 control-label"><spring:message code="store.name"></spring:message></label>
	    <div class="col-sm-3">
	      <input type="text" class="form-control required" id="name" name="name" maxlength="50" placeholder="<spring:message code="store.name"></spring:message>">
	    </div>
	  </div>
	  
	   <div class="form-group">
	    <label class="col-sm-2 control-label"><spring:message code="store.type"/></label>
	    <div class="col-sm-3">
			<select class="form-control" id="osw" name="osw">
				<c:choose>
					<c:when test="${!empty store}">
						<c:forEach var="code" items="${oswList}">
							<c:choose>
								<c:when test="${code.code eq store.osw}">
									<option value="${code.code}" selected="selected">${code.name}</option>
								</c:when>
								<c:otherwise>
									<option value="${code.code}">${code.name}</option>
								</c:otherwise>
							</c:choose>
						</c:forEach>
					</c:when>
					<c:otherwise>
						<c:forEach var="code" items="${oswList}">
							<option value="${code.code}">${code.name}</option>
						</c:forEach>
						
					</c:otherwise>
				</c:choose>

            </select>

		</div>
	  </div>
	  
	   <div class="form-group">
	    <label class="col-sm-2 control-label"><spring:message code="store.addr"/></label>
	    <div class="col-sm-3">
	      <input  class="form-control" id="addr" maxlength="50" name="addr" placeholder="<spring:message code="store.addr"/>">
	      
	    </div>
	  </div>
	  
	  <div class="form-group">
	    <label class="col-sm-2 control-label"><spring:message code="store.tel"/></label>
	    <div class="col-sm-3">
	      <input class="form-control" type="text" id="tel" maxlength="50" name="tel" placeholder="<spring:message code="store.tel"/>">
	    </div>
	  </div>
	 
	  <div class="form-group">
	    <div class="col-sm-offset-0 col-sm-1">
	      <button type="submit" class="btn btn-primary"><spring:message code="button.save"/></button>
	    </div>
	    <div class="col-sm-offset-0 col-sm-1">
	      <button type="reset" class="btn btn-info"><spring:message code="button.reset"/></button>
	    </div>
	    <div class="col-sm-offset-2 col-sm-1">
	    	<button type="button" class="btn btn-default" onclick="javascript:self.location='<%=g_basePath%>/storemgr/storeList.htm'"><spring:message code="button.cancel"/></button>
	    </div>
	  </div>
	 
	 </div>
	
	</form>
	
	<!-- ui-dialog -->
    <div id="modal-simple" title="Information" >
    	<div class="alert alert-success">
    		<label style="text-align: center;"><spring:message code="store.save.success"/></label>
    		
    	</div>
    </div>
</body>
</html>
